/**
 *  TextMash - simple IDE for Clojure
 *  
 *  Copyright (C) 2010 Aleksander Naszko
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package pl.olek.jruce;


/**
 *
 * @author anaszko
 */
public abstract class Scheduled {

    Daemon daemon;

    boolean fired;

    long frequency;

    long passed;

    public abstract void run();

    public void end() {
    }

//    public static void main(String[] args) {
//        Scheduled s = new Scheduled(1000) {
//
//            @Override
//            public void run() {
//                System.out.println("Doing at " + getFrequency());
//            }
//        };
//        Daemon.sleep(10000);
//        s.adjust(2000);
//        Daemon.sleep(10000);
//
//    }

    public Scheduled(long freq) {

        this.frequency = freq;

        daemon = new Daemon() {

            @Override
            public void run() {
                while (isRunning()) {
                    try {
                        synchronized (Scheduled.this) {
                        	if (!fired) {
                        		Scheduled.this.wait(frequency - passed);
                        	}
                            fired = false;
                        }
                    } catch (Exception e) {
//                        Logger.getLogger(Scheduled.class.getName()).log(Level.SEVERE, null, e);
                    }
                    
                    long now = System.currentTimeMillis();
                    if (isRunning()) {
                        Scheduled.this.run();
                    } else {
                        break;
                    }
                    synchronized (Scheduled.this) {
                        passed = System.currentTimeMillis() - now;
                        if (passed > (frequency / 2)) {
                            passed = 0;
                        }
                    }
                }
                end();
            }
        };
    }

    public synchronized void adjust(long frequency) {
        this.frequency = frequency;
        passed = 0;
    }

    public synchronized long getFrequency() {
        return frequency;
    }

    public void join() {
        daemon.join();
    }

    public synchronized void fire() {
    	fired = true;
        notify();
    }

    public void stop() {
        synchronized (this) {
            daemon.stop();
            notify();
        }
        daemon.join();
    }
}
